package com.sky.common.endpoint.sort;

import com.sky.business.sys.domain.CameraConfig;
import com.sky.common.utils.GeoUtils;
import com.sky.common.utils.NumberUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 排序
 *
 */
public class DistanceTrackAbilitySorter implements TrackAbilitySorter {


    /**
     * 计算设备和无人机的距离
     *
     * @param cameraConfig 设备
     * @param uavLng       无人机经度
     * @param uavLat       无人机维度
     * @param uavAltitude  无人机海拔
     * @return 直线距离
     */
    public double getUavDistance(CameraConfig cameraConfig, double uavLng, double uavLat, double uavAltitude) {
        double cameraLat = NumberUtil.parseDouble(cameraConfig.getCameraLatitude());
        double cameraLng = NumberUtil.parseDouble(cameraConfig.getCameraLongitude());
        double cameraAltitude = NumberUtil.parseDouble(cameraConfig.getCameraAltitude());
        double groundDistance = GeoUtils.calculateDistance(cameraLat, cameraLng, uavLat, uavLng);
        double altitudeDistance = Math.abs(uavAltitude-cameraAltitude);
        return Math.sqrt(Math.pow(groundDistance,2)+Math.pow(altitudeDistance,2));
    }


    @Override
    public List<CameraConfig> sort(List<CameraConfig> cameraConfigList, double uavLng, double uavLat, double uavAltitude) {
        List<CameraDistance> cameraDistanceList = new ArrayList<>();
        for (CameraConfig cameraConfig : cameraConfigList) {
            double distance = getUavDistance(cameraConfig, uavLng, uavLat, uavAltitude);
            CameraDistance cameraDistance = new CameraDistance(cameraConfig,distance);
            cameraDistanceList.add(cameraDistance);
        }
        Collections.sort(cameraDistanceList);
        return cameraDistanceList.stream().map(CameraDistance::getCameraConfig).collect(Collectors.toList());
    }

}
